package com.meals.system.api.system;

import com.meals.system.api.system.vo.WhitesMealSearchSellerVO;
import com.meals.system.api.system.vo.WhitesMealSellerDetailVO;
import com.meals.system.common.Constants;
import com.meals.system.common.SystemException;
import com.meals.system.config.annotation.TokenToSystemUser;
import com.meals.system.entity.*;
import com.meals.system.service.WhitesMealSellerInfoService;
import com.meals.system.service.WhitesMealSellerService;
import com.meals.system.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description:
 * Parm:
 * return:
 * Author: Chen
 * Date: 2021/12/10
 */
@RestController
@Api(value = "v1", tags = "0-3.白餐系统商家页面相关接口")
@RequestMapping("/user-api/v1")
public class WhitesMealSystemSellersAPI {

    private static final Logger logger = LoggerFactory.getLogger(WhitesMealSystemSellersAPI.class);

    @Resource
    private WhitesMealSellerInfoService whitesMealSellerInfoService;

    @GetMapping("/seller/search")
    @ApiOperation(value = "商家搜索接口", notes = "根据关键字和分类id进行搜索")
    public Result<PageResult<List<WhitesMealSearchSellerVO>>> search(@RequestParam(required = false) @ApiParam(value = "搜索关键字") String keyword,
                                                                     @RequestParam(required = false) @ApiParam(value = "分类id") Long sellerCategoryId,
                                                                     @RequestParam(required = false) @ApiParam(value = "页码") Integer pageNumber,
                                                                     @TokenToSystemUser SystemUser loginSystemUser){
        logger.info("seller search api, keyword={},sellerCategoryId={},pageNumber={},userId={}",keyword,sellerCategoryId,pageNumber,loginSystemUser.getUserId());

        Map params = new HashMap(8);
        // 两个搜索参数: 商家分类id和关键字
        // 两个搜索参数都为空，直接返回异常
        if (sellerCategoryId == null && StringUtils.isEmpty(keyword)){
            SystemException.fail("搜索参数异常");
        }
        if (pageNumber == null || pageNumber < 1){
            pageNumber = 1;
        }
        params.put("sellerCategoryId", sellerCategoryId);
        params.put("page", pageNumber);
        params.put("limit", Constants.SELLER_SEARCH_PAGE_LIMIT);
        // 对keyword做过滤 去掉空格
        if (!StringUtils.isEmpty(keyword)){
            params.put("keyword",keyword);
        }
        // 封装商家数据
        PageQueryUtil pageUtil = new PageQueryUtil(params);
        return ResultGenerator.genSuccessResult(whitesMealSellerInfoService.searchWhitesMealSeller(pageUtil));
    }


    @GetMapping("/seller/detail/{sellerId}")
    @ApiOperation(value = "商家详情接口",notes = "传参为商家id")
    public Result<WhitesMealSellerDetailVO> sellerDetail(@ApiParam(value = "商家id") @PathVariable("sellerId") Long sellerId,@TokenToSystemUser SystemUser loginSystemUser){
        logger.info("seller detail api, sellerId={}, userId={}",sellerId,loginSystemUser.getUserId());
        if (sellerId < 1){
            return ResultGenerator.genFailResult("商家参数异常");
        }
        SystemSellerInfo seller = whitesMealSellerInfoService.getWhitesMealSellerById(sellerId);
        if (seller == null){
            return ResultGenerator.genFailResult("参数异常");
        }
        WhitesMealSellerDetailVO sellerDetailVO = new WhitesMealSellerDetailVO();
        BeanUtil.copyProperties(seller, sellerDetailVO);
        sellerDetailVO.setSellerCarouseList(seller.getSellerCarousel().split(","));
        return ResultGenerator.genSuccessResult(sellerDetailVO);
    }
}

